/**
 * tandelin  2017-07-06
 */
//点击展示所有热评新闻
var request = 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MDE5ODkyNjYsInVzZXJJZCI6IjE1d2F5NXh4bmRjM29ld3VvZ2N6In0.K41L7hgqsPR-GPSN1jNDd4WWJ2BlEifAHSYvO3teAHY';
    // getParam = window.location.search,
    // getId = getParam.split('&')[0],
    // requestId = getId.substring(4),
function getUrlParam(id){
    var reg = new RegExp("(^|&)" + id + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null){
        return decodeURI(r[2]);
    }
    return null;
  }

var param = {
        postId: getUrlParam('id')
    };
$('body').hide();
// console.log(requestId);
// console.log(getId);
console.log(window.location.href);
function ajax(url, param) {
    return $.ajax({
        url: url,
        data: param || {},
        type: 'POST',
        dataType: "json",
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", localStorage.token)
        }
    });
};

/*
 * 获取新闻列表信息
 */
ajax("../news/posts/getPostsDtlById",param).done(function (resp){
    var data = resp,
        //headerHtml = $('head').html(),
        newsTime = new Date(data.data.showDate*1000),
        showTime = [newsTime.getFullYear(),(newsTime.getMonth()+1)<10? '0'+ (newsTime.getMonth()+1):(newsTime.getMonth()+1),newsTime.getDate()<10? '0'+newsTime.getDate():newsTime.getDate(),newsTime.getHours()<10? '0'+newsTime.getHours():newsTime.getHours(),newsTime.getMinutes()<10?'0'+newsTime.getMinutes():newsTime.getMinutes(),newsTime.getSeconds()<10? '0'+newsTime.getSeconds():newsTime.getSeconds()],
        time = showTime[0]+ '-' +showTime[1]+ '-'+showTime[2]+ ' '+showTime[3]+ ':' +showTime[4]+ ':' +showTime[5];
    $('.firstTitle').text(data.data.postTitle);
    $('.newsTime').text(time);
    $('.newsFrom').text(data.data.authors.name);
    $('.newsHeaderImg img').attr('src',data.data.authors.img);
    if(data.data.tpl == 'article_news' || data.data.tpl == 'article_news_text'){
        $('.content').html(data.data.postContent);
    }else if(data.data.tpl == 'article_video'){
        var key = JSON.parse(resp.data.postContent),
            template = $('#videoTemplate').clone().html().replace('movie.ogg',key.video_url);
        $('.content').append(template);
    }else if(data.data.tpl == 'article_pics'){
        var key = JSON.parse(resp.data.postContent),
            news = key.img_news;
        for(var i=0;i<news.length;i++){
            var template = $('#picsTemplate').clone().html().replace('pics',news[i].img).replace('这是一段文本',news[i].content);
            $('.content').append(template)
        }
    }else if(data.data.tpl == 'article_pics_list'){
        var key = JSON.parse(resp.data.postContent),
            news = key.img_news;
        for(var i=0;i<news.length;i++){
            var template = $('#picsList').clone().html().replace('pics',news[i]);
            $('.content').append(template)
        }
    }
    $('body').show();
    //$('head').html(headerHtml + data.data.header);
});